﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using CodeFarmer.Model.Entities;
using CodeFarmer.Service;

namespace CodeFarmer.Controller
{
    public class UserContext
    {
        /// <summary>
        /// 获取指定的用户上下文。
        /// </summary>
        public readonly static UserContext Instance = new UserContext();
        protected const string CODEFARMER_USER_SESSION = "CODEFARMER_USER_SESSION";

        private UserContext()
        {
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        public User CurrentUser
        {
            get
            {
                if (IsAuthenticated)
                {
                    User user = null;
                    if (HttpContext.Current.Session == null || HttpContext.Current.Session[CODEFARMER_USER_SESSION] == null)
                    {
                        user = DependencyResolver.Current.GetService<IUserService>().GetUser(HttpContext.Current.User.Identity.Name);
                        HttpContext.Current.Session[CODEFARMER_USER_SESSION] = user;
                    }
                    user = (User)HttpContext.Current.Session[CODEFARMER_USER_SESSION];
                    if (user != null)
                    {
                        if (user.ID.ToString() != HttpContext.Current.User.Identity.Name)
                        {
                            user = DependencyResolver.Current.GetService<IUserService>().GetUser(HttpContext.Current.User.Identity.Name);
                            HttpContext.Current.Session[CODEFARMER_USER_SESSION] = user;
                        }
                    }
                    return user;
                }
                else
                    return null;
            }
        }

        /// <summary>
        /// 是否已登录
        /// </summary>
        public Boolean IsAuthenticated
        {
            get
            {
                return HttpContext.Current.User.Identity.IsAuthenticated;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="RememberMe">创建持久 Cookie（跨浏览器会话保存的 Cookie）</param>
        public void SignIn(string UserName, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(UserName))
                throw new ArgumentException("值不能为 null 或为空。", "UserName");
            FormsAuthentication.SetAuthCookie(UserName, createPersistentCookie);
        }

        /// <summary>
        /// 注销
        /// </summary>
        public void SignOut()
        {
            FormsAuthentication.SignOut();
            HttpContext.Current.Session.Remove(CODEFARMER_USER_SESSION);
            HttpContext.Current.Session.RemoveAll();
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Request.Cookies.Clear();
        }
    }
}
